import { wxRequest } from '../../../utils/server.js';


const _wxRequest = new wxRequest();

const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    id: '',
    supplyItem: {}, //供应商
    devTypeItem: {} //设备类型
  },

  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getDetail()
    } else {
      this.getSupplierList()
    }
  },


  onShow: function () {
  },

  handleChoose: function (e) {
    var params = e.currentTarget.dataset

    if (!this.data.supplyItem.id && params.name == 'typeName') {
      wx.showToast({
        icon: 'none',
        title: '请选先选择供应商',
      })
      return
    }

    if (params.name == 'supName') {
      this.setData({
        devTypeItem: {
          id: 0,
          typeName: '请选择设备类型'
        }
      })
    }

    wx.navigateTo({
      url: '/pages/publish/template/commonlist?apiurl=' + params.apiurl + '&name=' + params.name + '&itemName=' + params.itemname + '&addUrl=' + params.addurl + '&param=' + params.param
    })
  },

  // 获取列表
  getSupplierList: function () {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/bizOrg/findSupplierList',
      method: 'POST',
      data: {}
    }, (res) => {
      var list = res.list
      if (list.length == 1) {
        that.setData({
          supplyItem: {
            id: list[0].id,
            supName: list[0].supName
          }
        })
      }
    })
  },

  formSubmit: function (e) {
    var formdata = e.detail.value;
    formdata.supId = this.data.supplyItem.id;
    formdata.supName = this.data.supplyItem.supName;
    formdata.display = formdata.display ? 1 : 0;
    formdata.hot = formdata.hot ? 1 : 0;
    formdata.devTypeId = this.data.devTypeItem.id
    formdata.devTypeName = this.data.devTypeItem.typeName

    if (!formdata.groupName) {
      wx.showToast({
        icon: 'none',
        title: '商品组名称不能为空',
      })
      return
    }
    if (!formdata.devTypeId) {
      wx.showToast({
        icon: 'none',
        title: '设备类型不能为空',
      })
      return
    }

    console.log('formdata:', formdata)
    this.formRequest(formdata)
  },

  //请求
  formRequest: function (formdata) {
    var url = '/gds/protected/comm/beanCrud/gds.GoodsGroupVO/create';
    var title = "发布成功"

    //修改
    if (this.data.cententDetail && this.data.cententDetail.id) {
      url = "/gds/protected/comm/beanCrud/gds.GoodsGroupVO/update";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/publish/goodsgroup/list',
        })
      }, 1500)
    })
  },


  goback: function () {
    wx.navigateBack({})
  },

  //获取内容
  getDetail: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/gds/protected/comm/beanCrud/gds.GoodsGroupVO/getById/" + this.data.id,
      method: 'get',
    }, (res) => {
      var data = res.vo
      this.setData({
        cententDetail: res.vo,
        supplyItem: {
          supName: data.supName,
          id: data.supId
        },
        devTypeItem: {
          id: data.devTypeId,
          typeName: data.devTypeName || '请选择设备类型'
        }
      })

    })
  }



})